package com.example.service.impl;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.config.cache.CacheDbTemplate;
import com.example.config.es.aspect.annotation.EsSync;
import com.example.entity.pojo.User;
import com.example.mapper.UserMapper;
import com.example.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service //<>里面的类：依次代表pojo类、Mapper类、id数据类型
public class UserCacheServiceImpl extends CacheDbTemplate<User, UserMapper, Long> implements UserService {
    @Override
    protected String getDatabaseName() {
        return "demo";
    }

    @Override
    protected String getTableName() {
        return "user";
    }

//    private UserMapper baseMapper=super.baseMapper; //如果嫌super.baseMapper麻烦则这样写


    // --------------------------------------------------------------


    private void a(User user) {
        super.baseMapper.insert(user); //只有数据库的操作，你不想用redis，那就用这个

        super.save(user); //有数据库和redis的操作
    }


    // --------------------------------------------------------------


    // 新增并同步到ES（指定强一致性）
    @EsSync(strategy = "strongConsistency", entityClass = User.class)
    @Transactional
    public void saveUser(User user) {
        super.baseMapper.insert(user);
    }

    // 更新并同步到ES（默认最终一致性）
    @EsSync(entityClass = User.class)
    @Transactional
    public void updateUser(User user) {
        super.baseMapper.updateById(user);
    }

    // 删除并同步到ES
    @EsSync(entityClass = User.class)
    @Transactional
    public void deleteUser(Long id) {
        super.baseMapper.deleteById(id);
    }
}
